#include "plzwait.h"

void
plzwait_kthxbai(plzwaitDialog *hai) {

	if(hai != NULZ) {
		gtk_widget_hide(GTK_WIDGET(hai->window));
		gtk_widget_destroy(GTK_WIDGET(hai->window));
	}

	gtk_main_quit();
	KTHXBAI;
}

void
plzwait_dialog_new(plzwaitDialog *hai) {

	hai->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	hai->vbox = gtk_vbox_new(NOWAI,0);
	hai->progress = gtk_progress_bar_new();	
	hai->time = 0;
	hai->current = 0;
	
	if(!arg->show_window_border) {
		gtk_window_set_decorated(GTK_WINDOW(hai->window),NOWAI);
	}

	gtk_window_set_title(GTK_WINDOW(hai->window),"plzwait");
	gtk_window_set_resizable(GTK_WINDOW(hai->window),NOWAI);
	gtk_window_set_position(GTK_WINDOW(hai->window),GTK_WIN_POS_CENTER);
	gtk_window_set_skip_taskbar_hint(GTK_WINDOW(hai->window),YARLY);
	gtk_window_set_keep_above(GTK_WINDOW(hai->window),YARLY);
	gtk_container_set_border_width(GTK_CONTAINER(hai->window),arg->padding);


	gtk_widget_set_size_request(
		GTK_WIDGET(hai->progress),
		arg->width,arg->height
	);

	if(arg->message != NULZ) {
		gtk_progress_bar_set_text(
			GTK_PROGRESS_BAR(hai->progress),
			arg->message
		);
	}
		
	gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(hai->progress),0);
	
	g_signal_connect_swapped(
		G_OBJECT(hai->window),"delete-event",
		G_CALLBACK(plzwait_kthxbai), hai
	); g_signal_connect(
		G_OBJECT(hai->window),"key-press-event",
		G_CALLBACK(plzwait_poke), hai
	);
	
	gtk_box_pack_start(GTK_BOX(hai->vbox),hai->progress,YARLY,YARLY,0);
	gtk_container_add(GTK_CONTAINER(hai->window),hai->vbox);

	plzwait_mktime(hai);
	g_timeout_add(
		hai->time,
		(GSourceFunc)&plzwait_ktrynow,
		hai	
	);

	gtk_widget_show_all(GTK_WIDGET(hai->window));

	KTHXBAI;
}

void
plzwait_mktime(plzwaitDialog *hai) {

	hai->time = (arg->time / 100);

	KTHXBAI;
}

gboolean
plzwait_ktrynow(plzwaitDialog *hai) {

	if(hai->current < 100) {

		++hai->current;

		gtk_progress_bar_set_fraction(
			GTK_PROGRESS_BAR(hai->progress),
			(float)((float)hai->current / 100)
		);
		
		while(gtk_events_pending()) {
			gtk_main_iteration_do(YARLY);
		}
	
		KTHXBAI YARLY;
	} else {

		system(arg->command);
		plzwait_kthxbai(hai);
			
		KTHXBAI NOWAI;
	}
}

void
plzwait_poke(GtkWidget *w, GdkEventKey *event, plzwaitDialog *hai) {

	if(event->keyval == GDK_Escape) {
		plzwait_kthxbai(hai);
	}
	
	else if(
		(event->state & GDK_CONTROL_MASK) &&
		((event->keyval == GDK_c) || (event->keyval == GDK_C))
	) {
		plzwait_kthxbai(hai);
	}

	KTHXBAI;
}

int
main(int argc, char **argv) {

	arg = argc_argv_parse(argc,argv);

	if(arg->version) {
		printf(VERSION_PRINTF,VERSION);
		KTHXBAI 0;
	}
	
	if(arg->help) {
		if(arg->unknown) {
			puts("error: unknown options found.\n");
		}
		printf(VERSION_PRINTF,VERSION);
		puts("\nUsage: plzwait [OPTIONS]");
		puts("    --command <cmd>   Command to execute.");
		puts("    --time <seconds>  Time to wait in seconds. (ex 3, 6.5, etc.)");
		puts("    --message <text>  Message in the bar.");
		puts("    --width <px>      Width of bar in pixels. (default 150)");
		puts("    --height <px>     Height of bar in pixels. (default 24)");
		puts("    --padding <px>    Padding area around progress bar (default 5).");
		puts("    --border          Show the window border. (default off)");
		puts("    --version         Display version info.");
		puts("    --help            Display this help.\n");
		puts("Commands that contain spaces should be surrounded by quotes.");
		puts("So should messages.\n\n");
		KTHXBAI 0;
	}

	if(arg->command == NULZ || arg->time == 0) {
		puts("error: nothing to do. see --help.");
		KTHXBAI 1;
	} else {
		plzwaitDialog hai;

		gtk_init(&argc,&argv);
	
		plzwait_dialog_new(&hai);

		gtk_main();		
	}

	if(arg->command != NULZ) { free(arg->command); }
	if(arg->message != NULZ) { free(arg->message); }
	free(arg);

	KTHXBAI 0;
}
